# ---------------------------------------------------------------------------
# Author: Min Feng
# Date: 01/04/08
# exports datasts following the requirements of UMD.
#----------------------------------------------------------------------------

import configure

_conf = configure.configure()

_base = _conf.getPathRepository()
_targ = _conf.getString('export4umd.path')

_rediance = ['NZT', 'MBZ']

import os
import re
import shutil

def belongs(c, f):
    for _c in c:
        if f[:3] == _c:
            return True

    return False
    
def createPath(t, p, r):
    _p1 = os.path.join(t, 'p' + p + 'r' + r)
    if os.path.exists(_p1) == False:
        os.mkdir(_p1)

    return _p1

_newlist = []

_oldlist = []
if _conf.getString('export4umd.old.list'):
    _oldlist = [_p.strip() for _p in file(_conf.getString('export4umd.old.list'), 'r').readlines()]

print 'skip', len(_oldlist), 'datasets'
for _root, _dirs, _files in os.walk(_base):
    for _f in _files:
        if _f not in _oldlist:
            if belongs(_rediance, _f):
                _m = _conf.matchDataset(_f)
                if _m:
                    _newlist.append(_f)
                    _name = _m.group(1) + _m.group(2) + _m.group(5) + _m.group(3) + _m.group(4) + '.zip'
                    print 'copying', _f, _name,
                    _path1 = os.path.join(_base, _f)
                    _path2 = os.path.join(createPath(_targ, _m.group(1), _m.group(2)), _name)
                    if os.path.exists(_path2):
                        if os.path.getsize(_path1) > os.path.getsize(_path2):
                            print 'should be replaced:', os.path.getsize(_path1), os.path.getsize(_path2)
                    else:
                        shutil.copyfile(_path1, _path2)
                    print 'ok'       

print len(_newlist), 'datasets were exported'
